/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { DiagnosingMatcher } from './DiagnosingMatcher.js';
/**
 * Supporting class for matching a feature of an object. Implement <code>featureValueOf()</code>
 * in a subclass to pull out the feature to be matched against.
 *
 * @param <T> The type of the object to be matched
 * @param <U> The type of the feature to be matched
 */
export class FeatureMatcher extends DiagnosingMatcher {
    /**
     * Constructor
     * @param subMatcher The matcher to apply to the feature
     * @param featureDescription Descriptive text to use in describeTo
     * @param featureName Identifying text for mismatch message
     */
    constructor(subMatcher, featureDescription, featureName) {
        super();
        this.subMatcher = subMatcher;
        this.featureDescription = featureDescription;
        this.featureName = featureName;
    }

    /**
     * Implement this to extract the interesting feature.
     * @param actual the target object
     * @return the feature to be matched
     */
    featureValueOf(actual) {}

    matchesWithDiagnosingMatcher(actual, mismatch) {
        let featureValue = this.featureValueOf(actual);
        if (!this.subMatcher.matches(featureValue)) {
            mismatch.appendText(this.featureName).appendText(" ");
            this.subMatcher.describeMismatch(featureValue, mismatch);
            return false;
        }
        return true;
    }
    describeTo(description) {
        description.appendText(this.featureDescription).appendText(" ")
            .appendDescriptionOf(this.subMatcher);
    }
}
